package com.vision.controller;

import com.github.pagehelper.Page;
import com.vision.pojo.vo.PageVO;
import com.vision.pojo.entity.Video;
import com.vision.pojo.AjaxResult;
import com.vision.pojo.vo.ReviewVideoVO;
import com.vision.pojo.vo.VideoAndUserVO;
import com.vision.pojo.vo.VideoVO;
import com.vision.service.VideoService;
import com.vision.utils.AliOssUtil;
import jakarta.servlet.http.HttpSession;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 添加视频
     * @param video 视频
     * @return 添加成功
     */
    @PostMapping("/addVideo")
    public AjaxResult addVideo(@RequestBody @Validated Video video) {
        videoService.addVideo(video);
        return AjaxResult.success();
    }

    /**
     * 删除视频
     * @param videoId 视频id
     * @return 成功
     */
    @DeleteMapping
    public AjaxResult deleteVideo (@NotNull Integer videoId) throws Exception {
        videoService.deleteVideo(videoId);
        return AjaxResult.success();
    }

    /**
     * 增加视频播放量
     * @param videoId 视频id
     * @return 成功
     */
    @PatchMapping("/addViewCount")
    public AjaxResult addViewCount (@NotNull Integer videoId) {
        videoService.addViewCount(videoId);
        return AjaxResult.success();
    }

    /**
     * 视频通过审核进入待确认状态
     * @param videoId 视频id
     * @return 成功
     */
    @PatchMapping("/passReviewToConfirm")
    public AjaxResult passReviewToConfirm (@NotNull Integer videoId) {
        videoService.updateReviewState(2,3,videoId);
        return AjaxResult.success();
    }

    /**
     * 视频未通过审核进入确认阶段
     * @param videoId 视频id
     * @return 成功
     */
    @PatchMapping("/notPassReview")
    public AjaxResult notPassReview (@NotNull Integer videoId) {
        videoService.updateReviewState(2,4,videoId);
        return AjaxResult.success();
    }

    /**
     * 视频通过审核
     * @param videoId 视频id
     * @return 成功
     */
    @PatchMapping("/passReview")
    public AjaxResult passReview (@NotNull Integer videoId) {
        videoService.updateReviewState(1,1,videoId);
        return AjaxResult.success();
    }

    /**
     * 更新视频
     * @param video 视频
     * @return 成功
     */
    @PutMapping("/updateVideo")
    public AjaxResult updateVideo (@RequestBody @Validated Video video) {
        videoService.updateVideo(video);
        return AjaxResult.success(null,"修改成功");
    }

    /**
     * 获取当前用户待审核的视频
     * @return 视频
     */
    @GetMapping("/getUnderReviewVideo")
    public AjaxResult<Video> getUnderReviewVideo() {
        Video video = videoService.getUnderReviewVideo();
        return AjaxResult.success(video);
    }

    /**
     * 获取所有待审核的视频
     * @return 视频列表
     */
    @GetMapping("/getAllUnderReviewVideo")
    public AjaxResult<PageVO<ReviewVideoVO>> getAllUnderReviewVideo(@NotNull Integer pageNum,@NotNull Integer pageSize) {
        return AjaxResult.success(videoService.getAllUnderReviewVideo(pageNum, pageSize));
    }

    /**
     * 获取视频列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param userId 用户id
     * @param state 视频状态
     * @return 视频列表
     */
    @GetMapping("/getVideoForPage")
    public AjaxResult<PageVO<Video>> getVideoForPage(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer state
    ) {
        PageVO<Video> videoPageDTO = videoService.getVideoForPage(pageNum, pageSize, userId, state);
        return AjaxResult.success(videoPageDTO);
    }

    /**
     * 获取视频列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param title 视频标题
     * @return 视频列表
     */
    @GetMapping("/getVideoForPageLimitByViewCount")
    public AjaxResult<PageVO<VideoAndUserVO>> getVideoForPageLimitByViewCount(
            @NotNull Integer pageNum,
            @NotNull Integer pageSize,
            @RequestParam(required = false) String title
    ) {
        return AjaxResult.success(videoService.getVideoForPageOrderByViewCount(pageNum, pageSize,title));
    }

    /**
     * 获取视频列表通过标签id
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param tagIds 标签id
     * @return 视频列表
     */
    @GetMapping("/getVideoForPageByTagId")
    public AjaxResult<PageVO<VideoAndUserVO>> getVideoForPageByTagId(
            @NotNull Integer pageNum,
            @NotNull Integer pageSize,
            @RequestParam(name = "tagIds") @NotNull Integer[] tagIds
    ) {
        return AjaxResult.success(videoService.getVideoForPageByTagId(pageNum,pageSize,tagIds));
    }

    /**
     * 根据视频id获取视频
     * @param videoId 视频id
     * @return 视频
     */
    @GetMapping("/getVideoById")
    public AjaxResult<VideoVO> getVideoById(@NotNull Integer videoId, HttpSession session) {
        // 1. 获取当前用户信息
        Map<String, Integer> claims = (Map<String, Integer>) session.getAttribute("claims");
        Integer userId = null;
        if (claims != null) {
            userId = claims.get("id");
        }
        // 2. 获取视频
        return AjaxResult.success(videoService.getVideoById(videoId,userId));
    }
}
